
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

#include "config.h"
#include <QStringList>
#include <QDir>

bool isNullOrEmpty(QString str)
{
	return str.isNull() || str.isEmpty();
}

QString getCurrentDir()
{
	QDir dir;
	return dir.currentPath();
}

int saveConfig(QString strGroupName, QStringList strKeys, QStringList strValues)
{
	return saveConfig(getCurrentDir(), "sysconfig", strGroupName, strKeys, strValues);
}

int saveConfig(QString strCfgFilePath, QString strGroupName, QStringList strKeys, QStringList strValues)
{
	return saveConfig(strCfgFilePath, "sysconfig", strGroupName, strKeys, strValues);
}

int saveConfig(QString strCfgFilePath, QString strCfgFileName, QString strGroupName, QStringList strKeys, QStringList strValues)
{
	int iRet = 0;
	
	do
	{
		if (strKeys.size() != strValues.size())
		{
			iRet = -1;
			break;
		}

		if (isNullOrEmpty(strCfgFilePath))
		{
			strCfgFilePath = getCurrentDir();
		}

		// 创建INI文件
		//QSettings settings(QSettings::IniFormat, QSettings::UserScope, 
		//	// QString("%1\\%2\\%3").arg(QCoreApplication::organizationName()).arg(QCoreApplication::applicationName()).arg("Qt5.5"), 
		//	strCfgFilePath, 
		//	strCfgFileName);
		QSettings settings(QString("%1/%2.ini").arg(strCfgFilePath, strCfgFileName), QSettings::IniFormat);

		if (isNullOrEmpty(strGroupName))
		{
			strGroupName = "DEFAULT";
		}
		strGroupName = strGroupName.toUpper();

		settings.beginGroup(strGroupName);
		int i = 0;
		for (; i < strKeys.size(); i++)
		{
			settings.setValue(strKeys[i].toUpper(), strValues[i]);
		}
		iRet = i;
		settings.endGroup();
		
	} while (false);
	return iRet;
}

QStringList loadConfig(QString strGroupName, QStringList strKeys)
{
	return loadConfig(getCurrentDir(), "sysconfig", strGroupName, strKeys);
}

QStringList loadConfig(QString strCfgFilePath, QString strGroupName, QStringList strKeys)
{
	return loadConfig(strCfgFilePath, "sysconfig", strGroupName, strKeys);
}

QStringList loadConfig(QString strCfgFilePath, QString strCfgFileName, QString strGroupName, QStringList strKeys)
{
	QStringList qslResult;

	do
	{
		if (strKeys.size() <= 0)
		{
			break;
		}

		if (isNullOrEmpty(strCfgFilePath))
		{
			strCfgFilePath = getCurrentDir();
		}

		// 创建INI文件
		//QSettings settings(QSettings::IniFormat, QSettings::UserScope, 
		//	strCfgFilePath, 
		//	strCfgFileName);
		QSettings settings(QString("%1/%2.ini").arg(strCfgFilePath, strCfgFileName), QSettings::IniFormat);

		if (isNullOrEmpty(strGroupName))
		{
			strGroupName = "DEFAULT";
		}
		strGroupName = strGroupName.toUpper();

		settings.beginGroup(strGroupName);
		int i = 0;
		for (; i < strKeys.size(); i++)
		{
			qslResult << settings.value(strKeys[i].toUpper()).toString();
		}

		settings.endGroup();

	} while (false);
	return qslResult;
}

// QList<QMap<QString, QString>> loadConfig(QString strCfgFilePath, QString strCfgFileName, QString strGroupName, QStringList strKeys, int iFlag)
QMap<QString, QString> loadConfig(QString strCfgFilePath, QString strCfgFileName, QString strGroupName, QStringList strKeys, int iFlag)
{
	// QList<QMap<QString, QString>> qListRet;
	QMap<QString, QString> mapRet;

	do
	{
		if (strKeys.size() <= 0)
		{
			break;
		}

		if (isNullOrEmpty(strCfgFilePath))
		{
			strCfgFilePath = getCurrentDir();
		}

		// 创建INI文件
		QSettings settings(QString("%1/%2.ini").arg(strCfgFilePath, strCfgFileName), QSettings::IniFormat);

		if (isNullOrEmpty(strGroupName))
		{
			strGroupName = "DEFAULT";
		}
		strGroupName = strGroupName.toUpper();

		settings.beginGroup(strGroupName);
		int i = 0;
		for (; i < strKeys.size(); i++)
		{
			mapRet.insert(strKeys[i], settings.value(strKeys[i].toUpper()).toString());
		}

		settings.endGroup();

	} while (false);

	return mapRet;
}

QString loadConfig(QString strGroupName, QString strKey)
{
	QMap<QString, QString> map = loadConfig(getCurrentDir(), "sysconfig", strGroupName, QStringList(strKey), 0);
	QString strResult = map[strKey];
	if (strResult.isNull())
	{
		strResult = "";
	}
	return strResult;
}


QMap<QString, QString> loadConfig(QString strGroupName, QStringList strKeys, int iFlag)
{
	return loadConfig(getCurrentDir(), "sysconfig", strGroupName, strKeys, iFlag);
}

int delConfig(QString strGroupName, QStringList strKeys)
{
	return delConfig(getCurrentDir(), QApplication::applicationName(), strGroupName, strKeys);
}

int delConfig(QString strCfgFilePath, QString strGroupName, QStringList strKeys)
{
	return delConfig(strCfgFilePath, QApplication::applicationName(), strGroupName, strKeys);
}

int delConfig(QString strCfgFilePath, QString strCfgFileName, QString strGroupName, QStringList strKeys)
{
	int iRet = 0;

	do
	{
		if (isNullOrEmpty(strCfgFilePath))
		{
			strCfgFilePath = getCurrentDir();
		}

		// 创建INI文件
		QSettings settings(QSettings::IniFormat, QSettings::UserScope, 
			strCfgFilePath, 
			strCfgFileName);

		if (isNullOrEmpty(strGroupName))
		{
			strGroupName = "DEFAULT";
		}
		strGroupName = strGroupName.toUpper();

		// keys是空 并且 group不为空, 则删除整个group
		if (strKeys.size() == 0) 
		{
			iRet = settings.allKeys().size();
			settings.beginGroup(strGroupName);
			settings.remove("");
			settings.endGroup();
			break;
		}
		
		// 删除键值
		settings.beginGroup(strGroupName);
		int i = 0;
		for (; i < strKeys.size(); i++)
		{
			settings.remove(strKeys[i].toUpper());
		}
		iRet = i;
		settings.endGroup();

	} while (false);
	return iRet;
}

